//
//  ViewController.swift
//  VastSwift
//
//  Created by BestVast on 2021/4/12.
//

import UIKit
import Kingfisher
import Alamofire
import HandyJSON
class ViewController: UIViewController {

    @IBOutlet weak var showIv: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let url: String = "https://avatars.githubusercontent.com/u/3429878?v=4"
        showIv.kf.setImage(with: URL(string: url))
        
        newworkRequest()
//        parseData()
        parseCat()
    }
    func parseCat() {
        class Cat: HandyJSON {
            var name: String?
            var id: String?

            required init() {}
        }

        let jsonArrayString: String? = "[{\"name\":\"Bob\",\"id\":\"1\"}, {\"name\":\"Lily\",\"id\":\"2\"}, {\"name\":\"Lucy\",\"id\":\"3\"}]"
        if let cats = [Cat].deserialize(from: jsonArrayString) {
            cats.forEach({ (cat) in
                debugPrint(cat!.name!)
            })
        }
    }
//接口：https://api.github.com/orgs/angular/members?page=1&per_page=5
//图片：https://avatars.githubusercontent.com/u/3429878?v=4
    let url = "https://api.github.com/orgs/angular/members?page=1&per_page=5"
    func newworkRequest() {
        class ItemData: HandyJSON {
            var login: String?
            var id: Int?
            var node_id: String?
            var avatar_url: String?
            var gravatar_id: String?
            var url: String?
            var html_url: String?
            var type: String?
            var site_admin: Bool = false
         
            required init() {}
        }
        
        AF.request(url).responseJSON { (response) in
            debugPrint(response.result)

        }
    }
    func parseData() {
        class BasicTypes: HandyJSON {
            var bool: Bool = true
            var intOptional: Int?
            var doubleImplicitlyUnwrapped: Double!
            var anyObjectOptional: Any?
         
            var arrayInt: Array<Int> = []
            var arrayStringOptional: Array<String>?
            var setInt: Set<Int>?
            var dictAnyObject: Dictionary<String, Any> = [:]
         
            var nsNumber = 2
            var nsString: NSString?
         
            required init() {}
        }
         
        let object = BasicTypes()
        object.intOptional = 1
        object.doubleImplicitlyUnwrapped = 1.1
        object.anyObjectOptional = "StringValue"
        object.arrayInt = [1, 2]
        object.arrayStringOptional = ["a", "b"]
        object.setInt = [1, 2]
        object.dictAnyObject = ["key1": 1, "key2": "stringValue"]
        object.nsNumber = 2
        object.nsString = "nsStringValue"
         
        let jsonString = object.toJSONString()!
         
        if let object = JSONDeserializer<BasicTypes>.deserializeFrom(json: jsonString) {
            // ...
            debugPrint(object);
        }
    }
}

